package com.kevin.service;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.nutz.dao.Cnd;
import org.nutz.ioc.aop.Aop;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;

import com.kevin.model.Message;
import com.kevin.validate.Errors;


/**
 * 
  * @ClassName: ArticleInfoService 
  * @Description: 文章
  * @author 吕凯
  * @date 2012-10-29 上午10:20:39 
  *
 */
@IocBean
public class MessageService extends BaseService{
	
	/**
	 * 获取留言列表
	 * 
	 * @return
	 */
	public void getMessageList(@Param("type")int type,HttpServletRequest request) {
		try {
			// 获取留言列表
			List<Message> aiList = basicDao.search(Message.class,Cnd.orderBy().desc("addDate"));
			
			request.setAttribute("dataList", aiList);
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("msg", "getFailure");
		}
	}
	
	/**
	 * 增 ： http://localhost:8080/nutztest/jsp/message/showMessageList.shtml
	 */
	@At
	@Ok("redirect:/message/showMessageList.shtml")
	@Aop("validationInterceptor")   
	public Message addMessage(@Param("..") Message ms,HttpServletRequest request,Errors es) {
		Message returnObj = null;
		System.out.println("^^123");		
		System.out.println("=="+es.errorCount()+" "+es.getErrorsMap());
		ms.setAddDate(new Date());
		ms.setAddIp(request.getRemoteAddr());
		if(es.errorCount() == 0)
			returnObj = basicDao.save(ms);
		return returnObj;
	}
	/**
	 * 审核
	 */
	@At
	@Ok("redirect:/message/showMessageList.shtml")
	public Boolean auditMessage(HttpServletRequest request,Errors es) {
		Boolean flag = false;
		try {
			Integer id = Integer.parseInt(request.getParameter("id"));
			Message data = basicDao.find(id,Message.class);
			data.setAudit(1);
			basicDao.update(data);
			flag = true;
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return flag;
	}
}
